<template>
  <div class="myISetUp">
    <div class="van-nav-bar">
      <div class="van-nav-bar-left"
           @click="back ">
        <van-icon size="16"
                  name="arrow-left"
                  color="#FFF" />返回
      </div>
      <div class="van-nav-bar-title">安全中心</div>
    </div>

    <div class="banner">
      <img class="auto-img"
           src="../assets/ruixing_a.png"
           alt="">
    </div>

    <div class="product">
      <div class="product-item"
           @click="getModify">
        <div>修改密码</div>

        <div>
          <van-icon name="arrow" />
        </div>

      </div>
      <div class="product-item"
           @click="getCancellation">
        <div>注销账号</div>
        <div>
          <van-icon name="arrow" />
        </div>
      </div>

      <!-- 修改密码 -->
      <van-popup v-model="Modify"
                 position="bottom"
                 closeable
                 :style="{ height: '30%' }">
        <div class="product-bottom">修改密码</div>

        <van-form>
          <van-field v-model="password"
                     name="新密码"
                     label="新密码"
                     placeholder="新密码"
                     autocomplete="off"
                     :rules="[{ required: true, message: '请填写新密码' }]" />

          <van-field v-model="oldPassword"
                     type="text"
                     name="旧密码"
                     label="旧密码"
                     placeholder="旧密码"
                     autocomplete="off"
                     :rules="[{ required: true, message: '请填写旧密码' }]" />

          <div class="van-button"
               @click="ModifyThe">
            <button class="vanButton">确定</button>
          </div>

        </van-form>
      </van-popup>

    </div>

    <van-button round
                color="#0C34BA"
                type="primary"
                block
                @click="logOut">退出登录</van-button>
  </div>
</template>

<script>
export default {
  name: 'MyISetUp',
  data () {
    return {
      Modify: false,
      
      RetrievePassword: false,
      // 注册时的手机号
      phone: '',
      // 新密码
      password: '',
      // 旧密码
      oldPassword: '',

      // 找回密码可见状态
      viewPassword: false
    }
  },
  methods: {



    back () {
      //返回上一级
      this.$router.go(-1);

    },
    // 修改密码弹窗
    getModify () {
      this.Modify = true
    },
    // 找回密码弹窗
    getRetrievePassword () {
      this.RetrievePassword = true
    },

    // 注销
    getCancellation () {
      this.$dialog.confirm({
        message: '是否注销? 一旦注销无法找回！'
      }).then(() => {
        this.axios({
          method: 'post',
          url: '/destroyAccount'
        }).then(result => {
          if (result.data.code == "G001") {
            const toast = this.$toast.loading({
              duration: 0, // 持续展示 toast
              forbidClick: true,
              message: '注销成功',
            });

            let second = 2;
            const timer = setInterval(() => {
              second--;
              if (second) {
                toast.message = `正在退出...`;

              } else {
                clearInterval(timer);
                // 手动清除 Toast
                this.$toast.clear();
                this.$router.push({ name: 'Login' })
              }
            }, 1000);


          }
        })
      })
    },

    // 修改密码
    ModifyThe () {
      this.$dialog.confirm({
        message: '是否修改密码?'

      }).then(() => {
        if (this.oldPassword == '' || this.password == '') {
          this.$toast('旧密码和新密码不能为空!')
          return
        }
        this.axios({
          method: 'post',
          url: '/updatePassword',
          data: {
            password: this.password,
            oldPassword: this.oldPassword
          }
        }).then(result => {

          if (result.data.code == "E001") {

            const toast = this.$toast.loading({
              duration: 0, // 持续展示 toast
              forbidClick: true,
              message: '修改密码成功',
            });

            let second = 1;
            const timer = setInterval(() => {
              second--;
              if (second) {
                toast.message = `正在退出...`;

              } else {
                clearInterval(timer);
                // 手动清除 Toast
                this.$toast.clear();
                this.$router.push({ name: 'Login' })
              }
            }, 1000);
          }

        })
      })
    },

    // 退出登录
    logOut () {
      this.$dialog.confirm({
        message: '是否退出登录?'
      }).then(() => {
        this.axios({
          method: 'post',
          url: '/logout',
        }).then(result => {

          if (result.data.code == "F001") {
            this.$toast.success('退出登录')

            const toast = this.$toast.loading({
              duration: 0, // 持续展示 toast
              forbidClick: true,
              message: '正在退出...',
            });

            let second = 1;
            const timer = setInterval(() => {
              second--;
              if (second) {
                toast.message = `正在退出...`;

              } else {
                clearInterval(timer);
                // 手动清除 Toast
                this.$toast.clear();
                this.$router.push({ name: 'Login' })
              }
            }, 1000);
            return;
          }

        })
      })
    }
  }

}
</script>

<style lang="scss" scoped>
.myISetUp {
  position: fixed;
  right: 0;
  left: 0;
  top: 60px;
  bottom: 0;
  .van-nav-bar {
    position: fixed;
    z-index: 999999;
    top: 0;
    left: 0;
    right: 0;
    padding-top: 30px;
    padding-bottom: 10px;
    display: flex;
    align-items: center;
    background-color: #0c34ba;
    color: #fff;

    .van-nav-bar-title {
      margin-left: 115px;
      font-size: 16px;
    }
    .van-nav-bar-left {
      font-size: 16px;
    }
  }
  .van-button {
    position: absolute;
    width: 320px;
    margin-left: 25px;
    bottom: 20px;
    .vanButton {
      margin-top: 20px;
      width: 100%;
      padding: 5px 10px;
      background-color: #0c34ba;
      border-radius: 20px;
      border-style: none;
      color: #fff;
      font-size: 18px;
    }
  }
  .banner {
    width: 100%;
    height: 180px;
  }
  .product {
    position: relative;
    top: -40px;
    background-color: #fff;
    display: flex;
    margin: 10px;
    padding: 10px;
    border-radius: 10px;
    flex-wrap: wrap;
    .product-item {
      //使最后一个没有右边框
      margin-top: 10px;
      width: 100%;
      background-color: #fff;
      margin-right: 10px;
      padding: 10px;
      position: relative;
      border-radius: 8px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      font-size: 16px;
    }
    .van-button {
      position: absolute;
      width: 320px;
      margin-left: 25px;
      bottom: 20px;
    }
  }
  .product-bottom {
    width: calc(100% - 20px);
    font-size: 16px;
    margin: 10px;
  }
}
</style>